﻿using rbac.Domain.Entity;
using rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rbac.Domain.Impl
{
    public class RoleRepository:IRoleRepository
    {
        private readonly RbacContext _context;

        public RoleRepository(RbacContext context)
        {
            _context = context;
        }
        public IQueryable<Role> SerrchRoles(string roleName)
        {
            var roles=_context.Roles.AsQueryable().Where(c=>c.IsDelete == false);
            if(!string.IsNullOrEmpty(roleName))
            {
                roles = roles.Where(c => c.ReleName.Contains(roleName));
            }
            return roles;
        }
        public IQueryable<Role> Roles()
        {
            var roles=_context.Roles; 
            return roles.AsQueryable();
        }
        public int Add(Role entity)
        {
            entity.IsDelete = false;
            _context.Roles.Add(entity);
            return _context.SaveChanges();

        }
        public int Update(Role entity)
        {
           var role=_context.Roles.FirstOrDefault(p=>p.Id == entity.Id);
            role.ReleName=entity.ReleName;
            role.Remark = entity.Remark;
            return _context.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int Delete( params int[] idList)
        {
            if (idList.Length <= 0)
            {
                return 0;
            }
            //查询要删除的实体集合
            var roles = _context.Roles.Where(c => idList.Contains(c.Id));
            foreach (var role in roles)
            {
                role.IsDelete = true;
                
            }
            return _context.SaveChanges();
        }
    }
}
